package org.ucas.faker.web.entity;

import org.ucas.faker.web.annotation.Registry;
import org.ucas.faker.web.exceptions.AppRegistryFailure;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

/**
 * Created by suemi on 17-7-7.
 */
@Entity
@Table
public class Application {

  static public enum Action {
    CREATE_GRAPH;
  }

  static public enum Status {
    ONLINE,
    OFFLINE
  }

  @Id
  @Column(columnDefinition = "VARCHAR(20)")
  private String code;

  @Column
  private Action action = Action.CREATE_GRAPH;

  @Column
  private Status status = Status.ONLINE;

  @Column(columnDefinition = "VARCHAR(30)")
  private String category;

  @NotNull
  @Column(columnDefinition = "VARCHAR(20)")
  private String subscriber; // 该服务负责人

  @Column(columnDefinition = "VARCHAR(255)")
  private String description;

  @Column(columnDefinition = "VARCHAR(100)")
  private String url;

  private String image;

  public static Application fromRegistry(Registry registry) {
    Application res = new Application();
    if("".equals(registry.code())) throw new AppRegistryFailure("Application code must be specified!");
    if("".equals(registry.subscriber())) throw new AppRegistryFailure("Application publication is not allowed to be anomalous!");
    res.code = registry.code();
    res.action = registry.action();
    res.category = registry.category();
    res.url = registry.url();
    res.status = registry.status();
    res.description = registry.desc();
    res.subscriber = registry.subscriber();
    return res;
  }

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public Action getAction() {
    return action;
  }

  public void setAction(Action action) {
    this.action = action;
  }

  public Status getStatus() {
    return status;
  }

  public void setStatus(Status status) {
    this.status = status;
  }

  public String getCategory() {
    return category;
  }

  public void setCategory(String category) {
    this.category = category;
  }

  public String getSubscriber() {
    return subscriber;
  }

  public void setSubscriber(String subscriber) {
    this.subscriber = subscriber;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }
}
